package com.yichat.micro.msghttpservice.core.base;

import com.yichat.micro.msghttpservice.core.retry.BatchRequestEvent;
import com.yichat.micro.msghttpservice.core.retry.RetryMsgHttpRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Created by chenmohan on 2019/04/18
 *
 * @description 用于接收批量请求, 并回调caller处理
 */
@Component
@Slf4j
public class BatchRequestEventHandler {

    @Value("${msg-http-service.protocol-http.impl}")
    String httpImpl;

    @Autowired
    ApplicationContext applicationContext;

    AbstractHttpCaller caller;

    @PostConstruct
    public void init() {
        caller = applicationContext.getBean(httpImpl, AbstractHttpCaller.class);
    }

    /**
     * @param e
     */
    @EventListener
    public void event(BatchRequestEvent e) {
        RetryMsgHttpRequestVo retryVo = e.getRetryVo();
        caller.process(retryVo);
    }

}
